package com.cfpl.admin_service.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cfpl.admin_service.VO.Role_Permissions;
import com.cfpl.admin_service.service.PermissionService;
import com.cfpl.admin_service.service.RoleService;
import com.cfpl.project_common.enums.CodeEnum;
import com.cfpl.project_common.exception.BusException;
import com.cfpl.project_common.pojo.Permission;
import com.cfpl.project_common.pojo.Role;
import com.cfpl.project_common.result.BaseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Auther: LYP
 * @Date: 2023/10/11 - 15:38
 * @Description: com.cfpl.admin_service.controller
 * @version: 1.0
 */
@RestController
@Slf4j
@RequestMapping("/admin/role")
@Api(tags = "管理员角色相关接口")
public class RoleController {

    @Autowired
    private RoleService roleService;
    @Autowired
    private PermissionService permissionService;

    /**
     * 分页查询 TestSuccess
     * @param currentPage 当前页
     * @param pageSize 每页显示条数
     * @return  page
     */
    @GetMapping("/pageQueryRole")
    @ApiOperation("分页查询角色信息")
    public BaseResult<Page<Role>> pageQueryRole(
            @RequestParam(defaultValue = "1") int currentPage,
            @RequestParam(defaultValue = "10") int pageSize
    ){
        Page<Role> rolePage = roleService.page(new Page<>(currentPage, pageSize));
        return BaseResult.ok(rolePage);
    }


    /**
     * 根据ID查询角色 TestSuccess
     * @param roleId 角色编号
     * @return role
     * 查询出来的角色信息中应该对已有角色进行突出显示
     */
    @GetMapping("/queryById/{id}")
    @ApiOperation("根据ID查询角色")
    public BaseResult<Role_Permissions> queryRole(@PathVariable("id") Long roleId){
        if (roleId == null){
            throw new BusException(CodeEnum.PARAMETER_ERROR);
        }
        //查询角色以及所属权限
        Role role = roleService.queryRoleInfoById(roleId);
        //所有权限列表
        List<Permission> permissionList = permissionService.list();
        Role_Permissions role_permissions = new Role_Permissions(role,permissionList);
        return BaseResult.ok(role_permissions);
    }

    /**
     * 添加角色 TestSuccess
     * @param role
     * role中的permissionList只给permissionID即可
     * @return status
     * 添加角色信息的同时可以把对应的权限信息一起添加到数据库中
     */
    @PostMapping("/insertRoleInfo")
    @ApiOperation("添加角色信息,权限只需要permissionID")
    public BaseResult insertRoleInfo(@RequestBody Role role){
        roleService.insertRoleInfo(role);
        return BaseResult.ok();
    }


    /**
     * 修改角色  TestSuccess
     * @param role role
     * @return status
     */
    @PostMapping("/updateRoleInfo")
    @ApiOperation("修改角色信息")
    public BaseResult updateRoleInfo(@RequestBody Role role){
        roleService.updateRoleInfo(role);
        return BaseResult.ok();
    }

    /**
     * 删除角色信息  TestSuccess
     * @param roleIds 角色编号
     * @return status
     */
    @ApiOperation("删除角色信息")
    @DeleteMapping("/deleteRoleInfo")
    public BaseResult deleteRoleInfo(@RequestParam List<Long> roleIds){
        if (roleIds == null || roleIds.isEmpty()){
            throw new BusException(CodeEnum.PARAMETER_ERROR);
        }
        roleService.removeRoleByIds(roleIds);
        return BaseResult.ok();
    }

    /** 这个接口可以用于修改权限使用 TestSuccess
     * 给角色分配权限
     * @param roleId 角色编号
     * @param permissionIds 分配权限编号数组
     * @return status
     * 先删再加
     */
    @ApiOperation("给角色分配(修改)权限")
    @PostMapping("/assignPermissionToRole")
    public BaseResult assignPermissionToRole(Long roleId,Long[] permissionIds){
        roleService.assignPermission(roleId, permissionIds);
        return BaseResult.ok();
    }


    /**
     * 展示所有的角色信息  TestSuccess
     * @return 所有角色列表
     */
    @GetMapping("/queryAllRolesInfo")
    @ApiOperation("查询所有的角色信息")
    public BaseResult<List<Role>> queryAllRolesInfo(){
        List<Role> roleList = roleService.list();
        return BaseResult.ok(roleList);
    }




}
